package com.binvshe.binvshe.organization.orgtype;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.image.IML;
import com.binvshe.binvshe.organization.entity.OrgTypeEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/8
 */


public class OrgTypeAdapter extends RecyclerViewBaseAdapter<OrgTypeEntity> {

    public int getIsSelectPosition() {
        return isSelectPosition;
    }

    public void setIsSelectPosition(int isSelectPosition) {
        this.isSelectPosition = isSelectPosition;
    }

    public int getSelectTypeID() {
        return selectTypeID;
    }

    public void setSelectTypeID(int selectTypeID) {
        this.selectTypeID = selectTypeID;
    }

    private int isSelectPosition = -1;
    private int selectTypeID = -1;

    public OrgTypeAdapter(Context con) {
        super(con);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_org_select_type, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
        VH mholder = (VH) holder;
        final OrgTypeEntity data = getData().get(position);
        IML.loadDetail(getContext(), mholder.mIvOrgType, data.getIcon());
        mholder.mTvContentType.setText(data.getTypeDesc());
        mholder.mTvTitleType.setText(data.getTypeName());
        if (position == getIsSelectPosition()) {
            mholder.mcbIsSelect.setChecked(true);
        } else {
            mholder.mcbIsSelect.setChecked(false);
        }
        setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setSelectTypeID(position+1);
                Log.e("被选中的data.getId()",position+1+"");
                setIsSelectPosition(position);
                notifyDataSetChanged();
            }
        });
    }


    public class VH extends RecyclerView.ViewHolder {
        @Bind(R.id.ivOrgType)
        ImageView mIvOrgType;
        @Bind(R.id.tvTitleType)
        TextView mTvTitleType;
        @Bind(R.id.tvContentType)
        TextView mTvContentType;
        @Bind(R.id.cbIsSelect)
        CheckBox mcbIsSelect;

        public VH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
